﻿using GGGK.Film.IRepository;
using GGGK.Film.IService;
using GGGK.Film.Models.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GGGK.Film.Service
{
    public class RoleMenuService : BaseService<Base_RoleMenu>, IRoleMenuService
    {
        private readonly IRoleMenuRepository repository;

        public RoleMenuService(IRoleMenuRepository repository, IBaseQueryRepository baseQuery) : base(repository, baseQuery)
        {
            this.repository = repository;
        }


        /// <summary>
        /// 授权添加
        /// </summary>
        /// <param name="roleMenus"></param>
        /// <returns></returns>
        public bool Add(int roleId, IEnumerable<Base_RoleMenu> roleMenus)
        {
            var data = Query(o=>o.RoleId == roleId);

            foreach (var item in data)
            {
                item.Status = 0;
                repository.Update(item);
            }

            if (roleMenus.Count() == 0) return repository.SaveChanges();

            foreach (var item in roleMenus)
            {
                repository.Add(item);
            }

            return SaveChanges();
        }
    }
}
